package com.anzia.study_6.slice;

import com.anzia.study_6.ResourceTable;
import com.anzia.study_6.data.City;
import com.anzia.study_6.data.ListItemProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;
import uitils.UIUtils;

import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice implements ListItemProvider.OnItemClickListener {

	private ListContainer listContainer;
	private ListItemProvider listItemProvider;

	@Override
	public void onStart(Intent intent) {
		super.onStart(intent);
		super.setUIContent(ResourceTable.Layout_ability_main);

		// 初始化布局
		initLayout();
		// 初始化列表里的数据
		initDatas();

	}

	// 真实项目中可以从着网络接口中获取数据
	private void initDatas() {
		ArrayList<City> cities = new ArrayList<>();
		cities.add(new City("1001", "北京"));
		cities.add(new City("1002", "上海"));
		cities.add(new City("1003", "广州"));
		cities.add(new City("1004", "天津"));
		cities.add(new City("1005", "成都"));
		cities.add(new City("1006", "杭州"));
		cities.add(new City("1007", "宜宾"));
		cities.add(new City("1008", "沈阳"));
		cities.add(new City("1009", "黑龙江"));
		cities.add(new City("1010", "廊坊"));
		cities.add(new City("1011", "新疆"));
		cities.add(new City("1012", "乌鲁木齐"));
		listItemProvider.setData(cities);
	}

	private void initLayout() {
		listContainer = (ListContainer) findComponentById(ResourceTable.Id_list);
		listItemProvider = new ListItemProvider(this, this);
		listContainer.setItemProvider(listItemProvider);
	}

	@Override
	public void onActive() {
		super.onActive();
	}

	@Override
	public void onForeground(Intent intent) {
		super.onForeground(intent);
	}

	@Override
	public void onItemClick(City city, int index) {
		// 点击了列表里的元素，执行这个方法
		UIUtils.showTip(this, city.getCityName()+"，"+city.getCityCode());
	}
}
